
package com.hlkj.pay.app.merchant.ext.impl.fuyou.utils;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hlkj.pay.app.merchant.ext.dto.access.MerchantAccessCallBackDto;
import com.hlkj.pay.app.merchant.ext.dto.access.MerchantBusinessCallBackDto;
import com.hlkj.pay.app.merchant.ext.impl.fuyou.dto.FuYouConfig;
import com.hlkj.pay.app.merchant.ext.impl.huifu.constant.HuiFuConstant;
import com.hlkj.pay.enums.MerchantEnum;
import com.huifu.bspay.sdk.opps.core.utils.RsaUtils;

/**
 * @author HlpayTeam
 * @date 2024/10/09 10:02
 */
public class FuYouCallBackParamsParseUtil {

    /**
     * 进件回调参数解析
     * 
     * @param callBackBody
     * @return
     */
    public static MerchantAccessCallBackDto accessCallBackParamsParse(String callBackBody) {
        MerchantAccessCallBackDto merchantAccessCallBackDto = new MerchantAccessCallBackDto();
        // 参数组装
        JSONObject response = JSON.parseObject(callBackBody);
        String respCode = response.get("resp_code").toString();
        if (HuiFuConstant.CALL_SUCCESS_CODE.equals(respCode)) {
            String sign = response.get("sign").toString();
            // 参数组装
            JSONObject dataObj = response.getJSONObject("data");
            // merchantAccessCallBackDto.setProcessStatus(CommonEnum.YES_NO_TYPE.YES);
            String reqSeqId = dataObj.get("req_seq_id").toString();
            merchantAccessCallBackDto.setRequestId(reqSeqId);
            String auditStatus = dataObj.get("audit_status").toString();
            // Y：审核通过，N：审核拒绝，F：失败，示例值：Y
            Object subRespDesc = dataObj.get("sub_resp_desc");
            switch (auditStatus) {
                case "Y":
                    merchantAccessCallBackDto.setAccessStatus(MerchantEnum.ACCESS_STATUS.WAIT_SET);
                    break;
                case "N":
                case "F":
                    merchantAccessCallBackDto.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
                    merchantAccessCallBackDto.setRemark("汇付审核失败,接口返回信息:" + subRespDesc.toString());
                    break;
            }
        }
        else {
            String respDesc = response.get("resp_desc").toString();
            merchantAccessCallBackDto.setProcessStatus(Boolean.FALSE);
            merchantAccessCallBackDto.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
            if (StringUtils.hasText(respDesc)) {
                merchantAccessCallBackDto.setRemark(respDesc);
            }
            else {
                merchantAccessCallBackDto.setRemark("斗拱进件回调信息错误信息为空,错误码为:" + respCode);
            }
        }
        return merchantAccessCallBackDto;
    }

    /**
     * 进件回调参数解析
     * 
     * @param huiFuConfig
     * @param callBackBody
     * @return
     */
    public static boolean verify(FuYouConfig huiFuConfig, String callBackBody) {
        // 参数组装
        JSONObject response = JSON.parseObject(callBackBody);
        String respCode = response.get("resp_code").toString();
        if (HuiFuConstant.CALL_SUCCESS_CODE.equals(respCode)) {
            String sign = response.get("sign").toString();
            // 参数组装
            JSONObject dataObj = response.getJSONObject("data");
            try {
                // 第一步 验证签名
                return RsaUtils.verify(dataObj.toJSONString(), huiFuConfig.getRsaPublicKey(), sign);
            }
            catch (Exception e) {
                return false;
            }
        }
        else {
            return false;
        }
    }

    /**
     * 业务开通参数解析
     * 
     * @param callBackBody
     * @return
     */
    public static MerchantBusinessCallBackDto businessCallBackParamsParse(String callBackBody) {
        MerchantBusinessCallBackDto merchantBusinessCallBackDto = new MerchantBusinessCallBackDto();
        // 参数组装
        JSONObject response = JSON.parseObject(callBackBody);
        String respCode = response.get("resp_code").toString();
        if (HuiFuConstant.CALL_SUCCESS_CODE.equals(respCode)) {
            // 参数组装
            JSONObject dataObj = response.getJSONObject("data");
            // merchantBusinessCallBackDto.setProcessStatus(CommonEnum.YES_NO_TYPE.YES);
            // 参数组装
            String reqSeqId = dataObj.get("req_seq_id").toString();
            merchantBusinessCallBackDto.setRequestId(reqSeqId);
            Object audit_status = dataObj.get("audit_status");
            if (audit_status != null) {
                String auditStatus = audit_status.toString();
                // P:审核中 Y：审核通过 N：审核拒绝 F：失败 C：拒签
                switch (auditStatus) {
                    case "P":
                        merchantBusinessCallBackDto.setAccessStatus(MerchantEnum.ACCESS_STATUS.WAIT_SETTING);
                        break;
                    case "Y":
                        merchantBusinessCallBackDto.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_SUCCESS);
                        break;
                    case "N":
                        String auditDesc = dataObj.get("audit_desc").toString();
                        merchantBusinessCallBackDto.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
                        merchantBusinessCallBackDto.setRemark("汇付业务开通审核拒绝,接口返回信息:" + auditDesc);
                        break;
                    case "F":
                        String subRespDesc = dataObj.get("sub_resp_desc").toString();
                        merchantBusinessCallBackDto.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
                        merchantBusinessCallBackDto.setRemark("汇付审核失败,接口返回信息:" + subRespDesc);
                        break;
                    case "C":
                        subRespDesc = dataObj.get("sub_resp_desc").toString();
                        merchantBusinessCallBackDto.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
                        merchantBusinessCallBackDto.setRemark("汇付审核拒签,接口返回信息:" + subRespDesc);
                        break;
                }
            }
        }
        else {
            String respDesc = response.get("resp_desc").toString();
            merchantBusinessCallBackDto.setProcessStatus(Boolean.FALSE);
            merchantBusinessCallBackDto.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
            if (StringUtils.hasText(respDesc)) {
                merchantBusinessCallBackDto.setRemark(respDesc);
            }
            else {
                merchantBusinessCallBackDto.setRemark("斗拱进件回调信息错误信息为空,错误码为:" + respCode);
            }
        }
        return merchantBusinessCallBackDto;
    }
}
